package com.dhp.security.utils;



import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

//生成token、解析token的工具类
public class TokenUtils {
    private static long tokenExpiration = 24*60*60*1000;
    private static String tokenSignKey = "123456";

    //根据用户名生成token
    public static String createToken(String username) {
        String token = Jwts.builder().setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .signWith(SignatureAlgorithm.HS512, tokenSignKey).compressWith(CompressionCodecs.GZIP).compact();
        return token;
    }

    //根据token得到用户名
    public static String getUserNameFromToken(String token) {
        String username = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().getSubject();
        return username;
    }


}
